package com.lepu.app.fun.chat.activity;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

import com.app.utils.LoginUtil;
import com.app.utils.barcode.CaptureActivity;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.PopUtil;
import com.core.lib.utils.main.UIHelper;
import com.core.lib.utils.main.UtilityBase;
import com.lepu.app.fun.chat.adapter.DoctorMyAdapter;
import com.lepu.app.fun.chat.bean.BeanDoctorMy;
import com.lepu.app.fun.chat.business.DoctorManager;
import com.lepu.app.fun.chat.util.ChatUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewRightButtonListener;
import com.lepu.pasm.R;

public class DoctorMyActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest,
		OnTopbarNewRightButtonListener {

	public static final String REQUEST_DATA = "request_data";
	public static final String REQUEST_DATA_ADD_SAO_YI_SAO = "request_data_add_sao_yi_sao";

	public static final int MSG_INIT_DATA = 10;

	private ListView mDataListView = null;
	private DoctorMyAdapter mDataAdapter = null;
	private List<BeanDoctorMy> mDataList = new ArrayList<BeanDoctorMy>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.chat_doctor_my_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void onTopbarRightButtonSelected() {
		showAddPop();
	}

	/**
	 * 初始化方法
	 */
	private void init() {
		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("我的医生");
		topbar.setonTopbarNewLeftLayoutListener(this);
		topbar.setRightText("添加医生");
		topbar.setOnTopbarNewRightButtonListener(this);

		mDataListView = (ListView) findViewById(R.id.dataListView);
		mDataListView.setOnItemClickListener(mOnItemClickListener);

		refreshData();
	}

	private void showHideNoDataLayout(boolean isShow) {
		LinearLayout noDataLayout = (LinearLayout) findViewById(R.id.noDataLayout);
		if (noDataLayout != null) {
			if (isShow) {
				if (mDataList == null || mDataList.size() == 0) {
					noDataLayout.setVisibility(View.VISIBLE);
				}else{
					noDataLayout.setVisibility(View.GONE);
				}
			} else {
				noDataLayout.setVisibility(View.GONE);
			}
		}
	}

	public void refreshData() {
		showProgressDialog();
		DoctorManager.getInstance().requestDoctorList(this);
	}

	@SuppressLint("InflateParams")
	private void showAddPop() {
		View view = LayoutInflater.from(this).inflate(
				R.layout.chat_my_doctor_add_pop, null);
		Button phoneAddButton = (Button) view.findViewById(R.id.phoneAddButton);
		Button saoYiSaoAddButton = (Button) view
				.findViewById(R.id.saoYiSaoAddButton);
		Button cancelButton = (Button) view.findViewById(R.id.cancelButton);

		phoneAddButton.setOnClickListener(mPopOnClickListener);
		saoYiSaoAddButton.setOnClickListener(mPopOnClickListener);
		cancelButton.setOnClickListener(mPopOnClickListener);

		PopUtil.getInstance().showPopWindow(this, null, view, Gravity.BOTTOM,
				null, false);
	}

	private void dismissAddPop() {

		PopUtil.getInstance().dismissPopWindow();
	}

	View.OnClickListener mPopOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = new Intent();
			int rid = v.getId();
			switch (rid) {
			case R.id.phoneAddButton:

				intent.setClass(DoctorMyActivity.this, DoctorAddActivity.class);
				startActivity(intent, true);
				break;
			case R.id.saoYiSaoAddButton:

				intent.setClass(DoctorMyActivity.this, CaptureActivity.class);
				intent.putExtra("flag", DoctorMyActivity.class.getName());
				startActivityForResult(intent, 100, true);
				break;
			case R.id.cancelButton:
				break;

			default:
				break;
			}
			dismissAddPop();
		}
	};

	private void bindDoctor(String data) {
		if (TextUtils.isEmpty(data) || data.length() < 32
				|| !ChatUtil.isRegexAccount(data)) {
			UIHelper.showToast(this, "二维码信息不正确！");
		} else {
			//发送通知
			String DoctorID = data.substring(32);
			if(!TextUtils.isEmpty(DoctorID)){
				ChatUtil.sendChatNotice(DoctorID);
			}
			//
			showProgressDialog();
			DoctorManager.getInstance().requestDoctorSendSaoYiSao(this, data);
		}
	}

	OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			BeanDoctorMy item = mDataList.get(position);

			Intent intent = new Intent(DoctorMyActivity.this,
					DoctorMyDetailActivity.class);
			intent.putExtra("DoctorID", item.DoctorID);
			startActivity(intent, true);
		}
	};

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_INIT_DATA:

				hideProgressDialog();

				mDataAdapter = new DoctorMyAdapter(DoctorMyActivity.this,
						mDataList);
				mDataListView.setAdapter(mDataAdapter);

				showHideNoDataLayout(true);
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_DATA)) {

			// no main thread
			List<BeanDoctorMy> dataList = BeanDoctorMy
					.parseListInfo((String) data);
			if (dataList != null && dataList.size() > 0) {
				DoctorManager.getInstance().saveDoctorsDB(dataList);
			}
			
			if(mDataList != null && mDataList.size() > 0)
			{
				mDataList.clear();
			}
			
			dataList = DoctorManager.getInstance().getDoctorListfromDB(
					UtilityBase.parseLong(LoginUtil.getUserId()));
			mDataList.addAll(dataList);
			mHandler.sendEmptyMessage(MSG_INIT_DATA);

		} else if (object.equals(REQUEST_DATA_ADD_SAO_YI_SAO)) {
			hideProgressDialog();

			refreshData();
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String data) {
		if (object.equals(REQUEST_DATA)) {
			mHandler.sendEmptyMessage(MSG_INIT_DATA);
		} else if (object.equals(REQUEST_DATA_ADD_SAO_YI_SAO)) {
			hideProgressDialog();
		}
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 100 && data != null) {
			String path = data.getStringExtra("data");
			bindDoctor(path);
		}
	}

}
